package com.clinic.action.report;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.clinic.action.common.AuthenticationAction;
import com.clinic.entity.gui.GuiDrug;
import com.clinic.entity.gui.GuiDrugStock;
import com.clinic.entity.param.Index;
import com.clinic.entity.type.ReportType;
import com.clinic.logic.drug.DrugLogic;
import com.clinic.logic.drug.DrugStockLogic;
import com.clinic.util.CommonUtil;

public class ReportAction extends AuthenticationAction {

	private static final long serialVersionUID = 8104929516555036621L;
	private static final Logger LOGGER = Logger.getLogger(ReportAction.class);
	
	@Autowired
	private DrugStockLogic drugStockLogic;
	@Autowired
	private DrugLogic drugLogic;
	
	private ReportType type;
	private List<GuiDrugStock> guiDrugStockList;
	private List<GuiDrug> guiDrugList;
	private int daysToPass = 10;
	private String subTitle;
	
	public String show() {
		if(isDoctorLogin() || isAssistantLogin()) {
			if (ReportType.DRUGSTOCKSTOBEEXPIRED == type) {
				Date todayDate = CommonUtil.getTodayDate();
				Date cutOffDate = CommonUtil.addDays(todayDate, daysToPass);
				guiDrugStockList = drugStockLogic.getToBeExpiredDrugStocksByDate(cutOffDate, new Index(100));
				subTitle = "to be expired in " + daysToPass + "days";
				return "drugstock";
			} else if (ReportType.DRUGSTOCKSEXPIRED == type) {
				Date todayDate = CommonUtil.getTodayDate();
				guiDrugStockList = drugStockLogic.getOutDatedDrugStocksByDate(todayDate, new Index(100));
				subTitle = "already expired";
				return "drugstock";
			} else if (ReportType.DRUGSACHIEVEMIN == type) {
				guiDrugList = drugLogic.getDrugsAchiveMinimum(new Index(100));
				return "drug";
			}
			return ActionResult.SUCCESS;
		}
		return ActionResult.AUTH_FAILED;
	}

	public ReportType getType() {
		return type;
	}

	public void setType(String type) {
		this.type = ReportType.getValue(type);
	}

	public boolean isExistGuiDrugStockList() {
		return guiDrugStockList != null && guiDrugStockList.size() > 0;
	}
	
	public List<GuiDrugStock> getGuiDrugStockList() {
		return guiDrugStockList;
	}

	public void setGuiDrugStockList(List<GuiDrugStock> guiDrugStockList) {
		this.guiDrugStockList = guiDrugStockList;
	}

	public boolean isExistGuiDrugList() {
		return guiDrugList != null && guiDrugList.size() > 0;
	}
	
	public List<GuiDrug> getGuiDrugList() {
		return guiDrugList;
	}

	public void setGuiDrugList(List<GuiDrug> guiDrugList) {
		this.guiDrugList = guiDrugList;
	}

	public int getDaysToPass() {
		return daysToPass;
	}

	public void setDaysToPass(int daysToPass) {
		this.daysToPass = daysToPass;
	}

	public String getSubTitle() {
		return subTitle;
	}

	public void setSubTitle(String subTitle) {
		this.subTitle = subTitle;
	}
	
}
